﻿
@using Single_SignOn.Models
@using Single_SignOn.ResX
@model Single_SignOn.Areas.Web.Models.LoginModels

@{
    ViewBag.Title = @ResourceTools.GetResource("Resource", "titleLogin");
    Layout = "~/Areas/Web/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("", "", new AjaxOptions { HttpMethod = "POST" }))
{
    @Html.ValidationSummary(true)
    <table style="width: 100%; height: 100%;  border: 0;">
        <tr>
            <td style="width: 100%;height: 100%; text-align: center; vertical-align: middle;">
        <div>
            <div id="content" > 
              <fieldset>
                  <legend><h4>@ResourceTools.GetResource("Resource","LoginHeader")</h4></legend>
                    <div class="clear"></div>    
                        <div class="left">
                            <p></p>
                            <p>
                                <div>
                                    <table style="width:100%;">
                                        <tr>
                                            <td style="width:80px;vertical-align: middle;" class="right">
                                                @Html.LabelFor(m => m.Username, new { @class = "label" }) :</td>
                                            <td  style="vertical-align: middle;" class="left">
                                                @Html.TextBoxFor(m => m.Username , new {@class = "textbox" ,@autocomplete="off"})
                                                <div class="error" ><div class="error-username"></div> </div>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </p>
                            <p></p>
                            <p>
                                 <div>
                                    <table style="width:100%;">
                                        <tr>
                                            <td style="width:80px;vertical-align: middle;" class="right">
                                                @Html.LabelFor(m => m.Password, new { @class = "label" }) :</td>
                                            <td style="vertical-align: middle;" class="left">
                                                @Html.TextBoxFor(m => m.Password, new { @class = "textbox", @type="password" })
                                                <div class="error" ><div class="error-password"></div> </div>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </p>
                            <p></p> 
                            <p>
                                <div>
                                    <table style="width:100%;">
                                        <tr>
                                            <td style="width:80px;" class="right">&nbsp;</td>
                                            <td class="left">
                                                 @Html.CheckBoxFor(m => m.RememberMe)
                                                 @Html.LabelFor(m => m.RememberMe)
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                
                               
                            </p> 
                            <p></p> 
                        </div>
                        <div class="center">
                            <table style="width:100%;text-align:center;">
                                <tr>
                                    <td style="width:50%;text-align:center;">
                                         <button type="button" name="Submit" id="Submit" title = "@ResourceTools.GetResource("Resource", "Submit")">
                                             <img src="~/Images/asset.gif" alt=" @ResourceTools.GetResource("Resource", "Submit") "/>
                                             &nbsp;&nbsp;&nbsp;&nbsp; @ResourceTools.GetResource("Resource", "Submit")
                                        </button> 
                                    </td>
                                    <td style="width:50%;text-align:center;">
                                        <button type="button" name="Cancel" id="Cancel" onclick=""  title = "@ResourceTools.GetResource("Resource", "Cancel")">
                                            <img src="~/Images/Delete.png" alt=" @ResourceTools.GetResource("Resource", "Cancel") "/>
                                            &nbsp;&nbsp;&nbsp;&nbsp; @ResourceTools.GetResource("Resource", "Cancel")
                                        </button> 
                                        
                                    </td>
                                </tr>
                            </table>
                        </div>  
                </fieldset> <div style="text-align:center;" class="error" >
                            <p></p> 
                                    <div class="error-summary"></div>
                            </div>      
            </div>      
        </div>
            </td>
        </tr>
    </table>
@*Html.Action("Access","Login",new ButtonModels{ _Id="#Back",_Type="button",_Action = "Index" ,_Controller = "Authentication" })*@
 <!-- Json passer -->
    <div class="display">
        <a id="AdLoginUrl" href="@Html.AttributeEncode(Url.Action("Index", "Authentication"))" style="display:none;"></a>
        
        <script type="text/javascript">
            $(document).ready(function () {

                $('#Username').first().focus();
                
                $('#Username').keypress(function (e) {
                    var code = (e.keyCode ? e.keyCode : e.which);
                    if (code == '13') {
                        $('#Password').focus();
                    }
                });
                
                $('#Password').keypress(function (e) {
                    var code = (e.keyCode ? e.keyCode : e.which);
                    if (code == '13') {
                        $('#Submit').click();
                    }
                });
                
                $('#Username').live('change',(function () {
                    //debugger;
                    var Username = $("#Username").val();
                    var Password = $("#Password").val();
                    var Remember = $('input[name=RememberMe]:checked').val();
                    $.getJSON("Login/GetCookies",
                        {
                            Username: Username,
                            Password: Password,
                            Remember: Remember
                        },
                        function (data) {
                            $('#Username').val(data.Name);
                            $('#Password').val(data.Pass);
                            $('input[name=RememberMe]').attr('checked', data.Remember);
                        });
                }));

                $('#Submit').click(function () {
                    var valid = true;
                    //*
                    $(".error-summary").html("");
                    $(".error-username").html("");
                    $(".error-password").html("");
                    //*
                    if ($("#Username").val() == "")
                    {
                        $(".error-username").html(" @ResourceTools.GetResource("Resource", "UsernameRequired") ");
                        valid = false;
                    }
                    if ($("#Password").val() == "")
                    {
                        $(".error-password").html(" @ResourceTools.GetResource("Resource", "PasswordRequired") ");
                        valid = false;
                    }

                    if (valid == true) {
                        var oSubmitDataObj = {
                            "Username": $("#Username").val(),
                            "Password": $("#Password").val(),
                            "Remember": $('input[name=RememberMe]:checked').val()
                        };
                        //debugger;
                        $.ajax({
                            url: '@Url.Action("Submit", "Login")',
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                            data: JSON.stringify(oSubmitDataObj),
                            success: function (data, textStatus, xhr) {
                                //At this point I would like to redirect
                                if (data.exception == "") {
                                    window.location.href = data.redirectToUrl;
                                }
                                else {
                                    //*
                                    $("#Password").val("");
                                    $(".error-username").html("");
                                    $(".error-password").html("");
                                    //*
                                    $(".error-summary").html("");
                                    $(".error-summary").html(data.exception);
                                }
                            },
                            error: function (xhr, textStatus, errorThrown) {
                                //*
                                $("#Password").val("");
                                $(".error-username").html("");
                                $(".error-password").html("");
                                //*
                                $(".error-summary").html(xhr.responseText);
                            }
                        });

                        return false;
                    }
                });

                // Clecr
                $('#Cancel').click(function () {
                    document.location.href = $('#AdLoginUrl').attr('href');
                    /*
                    $("#Username").val("");
                    $("#Password").val("");
                    $(".error-summary").html("");
                    $(".error-username").html("");
                    $(".error-password").html("");
                    $('input[name=RememberMe]').attr('checked', false);
                    */
                });
            });

        </script>
    </div>
    
}